package com.mindspore.federatedime.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.mindspore.federatedime.R;
import com.mindspore.federatedime.bean.ImageMsgBody;
import com.mindspore.federatedime.bean.Message;
import com.mindspore.federatedime.bean.MsgType;
import com.mindspore.federatedime.bean.TextMsgBody;
import com.mindspore.federatedime.util.BitmapUtil;
import com.mindspore.federatedime.util.ImageSize;
import com.mindspore.federatedime.widget.BubbleImageView;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<Message> data = new ArrayList<>();

    public ChatAdapter(Context context, List<Message> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == MsgType.TEXT_SEND) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text_send, parent, false);
            return new TextViewHolder(view);
        } else if (viewType == MsgType.TEXT_RECEIVE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text_receive, parent, false);
            return new TextViewHolder(view);
        } else if (viewType == MsgType.IMAGE_SEND) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_send, parent, false);
            return new ImageViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TextViewHolder) {
            TextMsgBody msgBody = (TextMsgBody) data.get(position).getBody();
            ((TextViewHolder) holder).chat_item_content_text.setText(msgBody.getMessage());
        } else if (holder instanceof ImageViewHolder) {
            ImageMsgBody msgBody = (ImageMsgBody) data.get(position).getBody();
            loadChatImage(msgBody.getThumbUrl(), ((ImageViewHolder) holder).bivPic);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getMsgType();
    }

    class TextViewHolder extends RecyclerView.ViewHolder {

        private TextView item_tv_time;
        private TextView chat_item_content_text;
        private ImageView chat_item_fail;
        private ProgressBar chat_item_progress;

        public TextViewHolder(@NonNull View itemView) {
            super(itemView);
            item_tv_time = itemView.findViewById(R.id.item_tv_time);
            chat_item_content_text = itemView.findViewById(R.id.chat_item_content_text);
            chat_item_fail = itemView.findViewById(R.id.chat_item_fail);
            chat_item_progress = itemView.findViewById(R.id.chat_item_progress);
        }
    }

    class ImageViewHolder extends RecyclerView.ViewHolder {
        private TextView item_tv_time;
        private ImageView bivPic;
        private ImageView chat_item_fail;
        private ProgressBar chat_item_progress;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            item_tv_time = itemView.findViewById(R.id.item_tv_time);
            bivPic = itemView.findViewById(R.id.bivPic);
            chat_item_fail = itemView.findViewById(R.id.chat_item_fail);
            chat_item_progress = itemView.findViewById(R.id.chat_item_progress);
        }
    }


    public void loadChatImage(int imgUrl, final ImageView imageView) {

//        final RequestOptions options = new RequestOptions()
//                .placeholder(R.mipmap.default_img_failed)
//                .error(R.mipmap.default_img_failed);

        Glide.with(context)
                .load(imgUrl)
                .centerCrop()
                .placeholder(R.color.white)
                .into(imageView);
//                .into(new SimpleTarget<Drawable>() {
//                    @Override
//                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
//                        ImageSize imageSize = BitmapUtil.getImageSize(((BitmapDrawable) resource).getBitmap());
//                        RelativeLayout.LayoutParams imageLP = (RelativeLayout.LayoutParams) (imageView.getLayoutParams());
//                        imageLP.width = imageSize.getWidth();
//                        imageLP.height = imageSize.getHeight();
//                        imageView.setLayoutParams(imageLP);
//
//                        Glide.with(context)
//                                .load(resource)
//                                .apply(options)
//                                .into(imageView);
//                    }
//                });
    }

}
